<template>
	<view>
		<u-navbar title="添加地址" placeholder bgColor="transparent" :autoBack="true">
		</u-navbar>
		<view class="mt-50">
			<view class="text-30 font-medium c_151515 ml-30">姓名</view>
			<view class="ml-30 mt-30">
				<input placeholder="请输入姓名" v-model="form.realName" class="text-36 font-medium c_151515" />
			</view>
			<view class="pd_0_30 mt-20">
				<u-gap height="1rpx" bgColor="#E7E7E7"></u-gap>
			</view>
		</view>
		<view class="mt-50">
			<view class="text-30 font-medium c_151515 ml-30">手机号</view>
			<view class="ml-30 mt-30">
				<input placeholder="请输入手机号" type="number" v-model="form.phone" class="text-36 font-medium c_151515" />
			</view>
			<view class="pd_0_30 mt-20">
				<u-gap height="1rpx" bgColor="#E7E7E7"></u-gap>
			</view>
		</view>
		<view class="mt-50">
			<view class="text-30 font-medium c_151515 ml-30">省/市/区</view>
			<view class="ml-30 mt-30">
				<input placeholder="请输入省/市/区地址" v-model="form.cityDetail" class="text-36 font-medium c_151515" />
			</view>
			<view class="pd_0_30 mt-20">
				<u-gap height="1rpx" bgColor="#E7E7E7"></u-gap>
			</view>
		</view>
		<view class="mt-50">
			<view class="text-30 font-medium c_151515 ml-30">详细地址</view>
			<view class="ml-30 mt-30">
				<input placeholder="请输入详细地址门牌号" v-model="form.detail" class="text-36 font-medium c_151515" />
			</view>
			<view class="pd_0_30 mt-20">
				<u-gap height="1rpx" bgColor="#E7E7E7"></u-gap>
			</view>
		</view>
		<view class="flex align-item_center ml-30 mt-30">
			<u-switch activeColor='#67B609' inactiveColo='#9B9B9B' v-model="form.isDefault" size='18'></u-switch>
			<view class="text-28 font-normal ml-30" style="color: #67B609;">设为默认</view>
		</view>
		<view @click="confirmLogic"
			class="newAddress mt-100 ml-30 text-center text-32 font-medium text-white flex align-item_center justify-content_center">
			立即{{isNewArea?'添加':'修改'}}
		</view>
	</view>
</template>

<script>
	import msg from "@/utils/msg.js"
	export default {
		data() {
			return {
				defaultAddress: true,
				form: {
					detail: "",
					realName: "",
					cityDetail: "",
					phone: undefined,
					isDefault: false,
				},
				loadOption: {},
			}
		},
		onLoad(option) {
			this.loadOption = option;
			if (!this.isNewArea) this.addressDetail()
		},
		computed: {
			isNewArea() {
				return this.loadOption.id === '-1'
			}
		},
		methods: {
			addressDetail() {
				this.$iApi.addressDetail(this.loadOption.id).then(res => {
					for (let key in this.form) {
						this.form[key] = res.data[key]
					}
					console.log(res.data);
				})
			},
			confirmLogic() {
				if (!this.form.realName.trim()) return msg.toast("请输入姓名")
				if (!this.form.phone) return msg.toast("请输入手机号")
				if (this.form.phone.length !== 11) return msg.toast("请输入11位手机号")
				if (!this.form.cityDetail) return msg.toast("请输入省市区")
				if (!this.form.detail) return msg.toast("请输入详细地址")

				this.$iApi.addressEdit({
					...this.form,
					id: this.isNewArea ? undefined : this.loadOption.id
				}).then(res => {
					if (this.isNewArea) msg.toast("添加成功")
					else msg.toast("修改成功")
					setTimeout(() => {
						uni.navigateBack()
					}, 2000)

				})

			}
		}
	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
	.newAddress {
		width: 690rpx;
		height: 98rpx;
		background: #67B609;
		border-radius: 100rpx 100rpx 100rpx 100rpx;
	}
</style>